﻿// DlgSetPile.cpp : 实现文件
//

#include "stdafx.h"
#include "GHCY.h"
#include "DlgSetPile.h"
#include "afxdialogex.h"


// DlgSetPile 对话框

IMPLEMENT_DYNAMIC(DlgSetPile, CDialogEx)

DlgSetPile::DlgSetPile(CWnd* pParent /*=NULL*/)
	: CDialogEx(DlgSetPile::IDD, pParent)
	, m_nXNumber(0)
	, m_nZNumber(0)
	, m_nXCurrent(0)
	, m_nZCurrent(0)
	, m_nNumber(0)
	, m_nType(POINT_TYPE::POINT_NONE)
	//, m_bActived(FALSE)
	, m_bDisabled(TRUE)	// 默认停用是TRUE，避免启动时还未连接，导航到停用点
	, m_nTray(0)
	, m_nSerial(0)
	, m_nNodeCard(0)
	, m_strProductID(_T(""))
{
}

DlgSetPile::~DlgSetPile()
{
}

void DlgSetPile::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT2, m_nTray);
	DDX_Text(pDX, IDC_EDIT3, m_nSerial);
	DDV_MinMaxInt(pDX, m_nSerial, 0, 1000);
	DDX_Text(pDX, IDC_EDIT4, m_nXCurrent);
	DDX_Text(pDX, IDC_EDIT5, m_nZCurrent);
	DDX_Text(pDX, IDC_EDIT1, m_nNumber);
	DDX_Text(pDX, IDC_EDIT13, m_nZNumber);
	DDX_Text(pDX, IDC_EDIT15, m_nXNumber);
	// 20181209 改变限制方法，因为可能从多层改到少层，超界不能继续
	//DDV_MinMaxInt(pDX, m_nNumber, 0, m_nXNumber * m_nZNumber);
	DDV_MinMaxInt(pDX, m_nNumber, 0, 10000);
	//DDX_CBString(pDX, IDC_COMBO1, m_strProductType);
	DDX_Text(pDX, IDC_EDIT7, m_strCode);
	DDX_Text(pDX, IDC_EDIT8, m_strNextCode);
	//DDX_Check(pDX, IDC_CHECK3, m_bActived);
	DDX_Check(pDX, IDC_CHECK4, m_bDisabled);
	//DDX_Control(pDX, IDC_CHECK3, m_pCheckActive);
	DDX_Control(pDX, IDC_CHECK4, m_pCheckDisabled);
	DDX_Control(pDX, IDC_SPIN1, m_pSpin);
	DDX_Text(pDX, IDC_EDIT14, m_nNodeCard);
	DDX_Text(pDX, IDC_EDIT16, m_strProductID);
}

BEGIN_MESSAGE_MAP(DlgSetPile, CDialogEx)
	ON_NOTIFY((UINT)UDN_DELTAPOS, IDC_SPIN1, &DlgSetPile::OnDeltaposSpin1)
	ON_BN_CLICKED(IDC_BUTTON1, &DlgSetPile::OnBnClickedButton1)
	ON_BN_CLICKED(IDOK, &DlgSetPile::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON2, &DlgSetPile::OnBnClickedButton2)
END_MESSAGE_MAP()


// DlgSetPile 消息处理程序


void DlgSetPile::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	
	UpdateData();

	m_nZCurrent -= pNMUpDown->iDelta;
	if( m_nZCurrent > m_nZNumber )
	{
		if( m_nXCurrent >= m_nXNumber )	// 排数和层数都到最大值
		{
			m_nXCurrent = m_nXNumber;
			m_nZCurrent = m_nZNumber;
		}
		else
		{
			m_nZCurrent = 1;
			m_nXCurrent++;
		}
	}
	else if( m_nZCurrent == 0 )
	{
		if( m_nXCurrent > 1 )
		{
			m_nXCurrent--;
			m_nZCurrent = m_nZNumber;
		}
		else
		{
			m_nXCurrent = 0;
		}
	}
	else if( m_nZCurrent < 0 )
	{
		m_nZCurrent = 0;
		m_nXCurrent = 0;
	}
	else if( m_nXCurrent == 0 )
	{
		m_nXCurrent = 1;
	}

	m_nNumber = (m_nXCurrent - 1) * m_nZNumber + m_nZCurrent;
	if( m_nNumber < 0 )
		m_nNumber = 0;

	UpdateData( FALSE );

	*pResult = 0;
}

void DlgSetPile::OnBnClickedButton1()
{
	m_nXCurrent = 0;
	m_nZCurrent = 0;
	m_nNumber = 0;

	UpdateData( FALSE );
}

// 20181209 ?? 细化修改方法
void DlgSetPile::OnBnClickedOk()
{
	//int xn = m_nXNumber;
	//int zn = m_nZNumber;

	int num = m_nNumber;

	if( !UpdateData() )
		return;

	//if( m_nNumber != 0 
	//	&& (xn != m_nXNumber
	//	|| zn != m_nZNumber) )
	//{
	//	AfxMessageBox( L"垛数不为0时，不能修改最大排数和层数。" );
	//	return;
	//}

	if (num == m_nNumber)
	{
		CDialogEx::EndDialog(IDOK);		// 20181209 如果托数没有改变，不算行数和层数，改变最大层数时不改变当前位置
		return;
	}

	// 根据输入的垛数求层数和排数
	ASSERT( m_nNumber >= 0 );
	ASSERT( m_nZNumber > 0 );

	m_nXCurrent = m_nNumber / m_nZNumber;
	m_nZCurrent = m_nNumber - m_nZNumber * m_nXCurrent;
	if( m_nZCurrent == 0 )
	{
		if( m_nNumber != 0 )
			m_nZCurrent = m_nZNumber;
	}
	else
	{
		m_nXCurrent++;
	}

	CDialogEx::EndDialog( IDOK );
}

// 填满库区
void DlgSetPile::OnBnClickedButton2()
{
	m_nXCurrent = m_nXNumber;
	m_nZCurrent = m_nZNumber;
	ASSERT( m_nXCurrent > 0 && m_nZCurrent > 0 );
	m_nNumber = (m_nXCurrent - 1) * m_nZNumber + m_nZCurrent;

	UpdateData( FALSE );
}


BOOL DlgSetPile::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	//if( m_nType != 3 )	// 取货点
	//	m_pCheckDisabled.EnableWindow( FALSE );	// 存货区不设停用

	// 设置均速加减
	UDACCEL a[2];//for 赋值  
	a[0].nSec = 1;//过1秒  
	a[0].nInc = 1;//增加1  
	m_pSpin.SetAccel(1, a);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
